﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CaliburnMicroLogger.cs" company="SingularityShift.com">
//   Copyright (c) 2011 SingularityShift.com. All rights reserved.
// </copyright>
// <author>Ben McClure</author>
// <email>ben@singularityshift.com</email>
// <date>3/26/2011</date>
// <summary>
//   Implements the caliburn micro logger class
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace FalloutModStudio
{
    #region Using Directives

    using System;

    using Caliburn.Micro;

    using Slf;

    #endregion

    /// <summary>
    /// Caliburn micro logger. 
    /// </summary>
    /// <remarks>
    /// Ben McClure, 3/26/2011. 
    /// </remarks>
    public class CaliburnMicroLogger : ILog
    {
        #region Constants and Fields

        /// <summary> The logger </summary>
        protected readonly ILogger _logger;

        /// <summary> The type of the object being logged </summary>
        protected readonly Type _type;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="CaliburnMicroLogger"/> class. 
        /// </summary>
        /// <remarks>
        /// Ben McClure, 3/26/2011. 
        /// </remarks>
        /// <param name="type">
        /// The type of object being logged. 
        /// </param>
        /// <param name="logger">
        /// The logger. 
        /// </param>
        public CaliburnMicroLogger(Type type, ILogger logger = null)
        {
            this._type = type;

            this._logger = logger ?? LoggerService.GetLogger();
        }

        #endregion

        #region Implemented Interfaces

        #region ILog

        /// <summary>
        /// Logs the exception.
        /// </summary>
        /// <param name="exception">
        /// The exception.
        /// </param>
        public void Error(Exception exception)
        {
            this._logger.Error(CreateLogMessage(exception.ToString()));
        }

        /// <summary>
        /// Logs the message as info.
        /// </summary>
        /// <param name="format">
        /// A formatted message.
        /// </param>
        /// <param name="args">
        /// Parameters to be injected into the formatted message.
        /// </param>
        public void Info(string format, params object[] args)
        {
            this._logger.Info(CreateLogMessage(format, args));
        }

        /// <summary>
        /// Logs the message as a warning.
        /// </summary>
        /// <param name="format">
        /// A formatted message.
        /// </param>
        /// <param name="args">
        /// Parameters to be injected into the formatted message.
        /// </param>
        public void Warn(string format, params object[] args)
        {
            this._logger.Warn(CreateLogMessage(format, args));
        }

        #endregion

        #endregion

        #region Methods

        /// <summary>
        /// Creates a log message. 
        /// </summary>
        /// <remarks>
        /// Ben McClure, 3/26/2011. 
        /// </remarks>
        /// <param name="format">
        /// A formatted message. 
        /// </param>
        /// <param name="args">
        /// Parameters to be injected into the formatted message. 
        /// </param>
        /// <returns>
        /// The formatted log message. 
        /// </returns>
        protected static string CreateLogMessage(string format, params object[] args)
        {
            return string.Format("[{0}] {1}", DateTime.Now.ToString("o"), string.Format(format, args));
        }

        #endregion
    }
}